﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Estimate.Common.DBFieldTypes;

namespace Estimate.Common.Repository
{
    /// <summary>
    /// 个体排名表数据库访问类
    /// </summary>
    public class IndividualRankRepository : BaseRepository
    {
        public IndividualRankRepository(string connectionString)
            : base(connectionString, "IndividualRank")
        { }

        public override string GetCreateTableSql()
        {
            string sqlcreate = String.Format("CREATE TABLE [{0}] (id bigint identity(1,1), startdate date,enddate date,forecasters varchar(2000),realtype varchar(8),  scorestring text) \n", _tableName);
            sqlcreate += String.Format("CREATE CLUSTERED INDEX [{0}_INDEX] ON [{1}] (startdate,enddate,realtype,forecasters) \n", _tableName, _tableName);
            sqlcreate += String.Format("alter table [{0}] add primary key (id)", _tableName);
            return sqlcreate;
        }


        /// <summary>
        /// 查看考核表是否存在
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="forecasters">排序后的预报员列表，以","分隔</param>
        /// <param name="realtype">实况类型</param>
        /// <returns>是否存在相应记录</returns>
        public bool IsRecordExist(DateTime startDate, DateTime endDate, string forecasters, ForeSemiType realtype)
        {
            string sql = String.Format("SELECT id FROM [{0}] WHERE startdate=@startdate  and enddate=@enddate and  realtype=@realtype and forecasters=@forecasters", _tableName);
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@forecasters",getSortedStrings( forecasters));
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            return DBHelper.Exists(_connectionString, sql, param);
        }

        /// <summary>
        /// 获取月度报表的字符串值
        /// </summary>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <param name="forecasters">排序后的预报员列表，以","分隔</param>
        /// <param name="realtype">实况类型</param>
        /// <returns>存储的报表记录</returns>
        public string GetScoreString(DateTime startDate, DateTime endDate, string forecasters, ForeSemiType realtype)
        {
            string sql = String.Format("SELECT scorestring FROM [{0}] where startdate=@startdate  and enddate=@enddate and  realtype=@realtype and forecasters=@forecasters", _tableName);
            SqlParameter[] param = new SqlParameter[4];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@forecasters", getSortedStrings(forecasters));
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            SqlConnection connection = new SqlConnection(_connectionString);
            SqlDataReader reader = DBHelper.ExecuteReader(connection, sql, param);
            if (reader.Read())
            {
                string scorestring = reader["scorestring"].ToString();
                connection.Close();
                return scorestring;
            }
            return "";
        }

        public void InsertScoreString(DateTime startDate, DateTime endDate, string forecasters, ForeSemiType realtype, string scoreString)
        {
            string sql = String.Format("INSERT INTO [{0}] (startdate,enddate,forecasters,realtype,scorestring) values(@startdate ,@enddate ,@forecasters,@realtype,@scorestring)", _tableName);
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@forecasters", getSortedStrings(forecasters));
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            param[4] = new SqlParameter("@scorestring", scoreString);
            DBHelper.ExecuteSql(_connectionString, sql, param);
        }

        public void UpdateScoreString(DateTime startDate, DateTime endDate, string forecasters, ForeSemiType realtype, string scoreString)
        {
            string sql = String.Format("UPDATE [{0}] SET scorestring=@scorestring WHERE startdate=@startdate  and enddate=@enddate and type=@type and regionname=@regionname and stations=@stations and realtype=@realtype ", _tableName);
            SqlParameter[] param = new SqlParameter[5];
            param[0] = new SqlParameter("@startdate", startDate.ToString("yyyyMMdd"));
            param[1] = new SqlParameter("@enddate", endDate.ToString("yyyyMMdd"));
            param[2] = new SqlParameter("@forecasters", getSortedStrings(forecasters));
            param[3] = new SqlParameter("@realtype", realtype.ToString());
            param[4] = new SqlParameter("@scorestring", scoreString);
            DBHelper.ExecuteSql(_connectionString, sql, param);
        }

        public void SetScoreString(DateTime startDate, DateTime endDate, string forecasters, ForeSemiType realtype, string scoreString)
        {
            if (IsRecordExist(startDate, endDate, forecasters, realtype))
                UpdateScoreString(startDate, endDate, forecasters, realtype, scoreString);
            else
            {
                InsertScoreString(startDate, endDate, forecasters, realtype, scoreString);
            }
        }
    }
}
